package com.nf.xiaomi.service.impl;

import com.nf.xiaomi.dao.EvaluateDao;
import com.nf.xiaomi.dao.ProductDao;
import com.nf.xiaomi.dao.UserDao;
import com.nf.xiaomi.dao.impl.EvaluateDaoImpl;
import com.nf.xiaomi.dao.impl.ProductDaoImpl;
import com.nf.xiaomi.dao.impl.UserDaoImpl;
import com.nf.xiaomi.entity.EvaluateEntity;
import com.nf.xiaomi.entity.OrderEntity;
import com.nf.xiaomi.entity.ProductEntity;
import com.nf.xiaomi.model.AttributeModel;
import com.nf.xiaomi.service.EvaluateService;
import com.nf.xiaomi.util.JsonUtils;

import java.time.LocalDateTime;
import java.util.List;

public class EvaluateServiceImpl implements EvaluateService {
    EvaluateDao evaluateDao = new EvaluateDaoImpl();
    ProductDao productDao = new ProductDaoImpl();
    UserDao userDao = new UserDaoImpl();
    @Override
    public List<EvaluateEntity> getAll() {
        List<EvaluateEntity> all = evaluateDao.getAll();
        getEvaluateAdditionalData(all);
        return all;
    }

    @Override
    public List<EvaluateEntity> getAll(Long account) {
        Integer userID = userDao.getAllID(account);
        List<EvaluateEntity> all = evaluateDao.getAll(userID);
        getEvaluateAdditionalData(all);
        return all;
    }

    @Override
    public List<EvaluateEntity> getAll(Integer evaluateID) {
        List<EvaluateEntity> all = evaluateDao.getAllEvaluateID(evaluateID);
        getEvaluateAdditionalData(all);
        return all;
    }

    @Override
    public int insert(Integer orderDetailsId,String evaluateContent, LocalDateTime evaluateDate, Integer nstate) {
        return evaluateDao.insert(orderDetailsId,evaluateContent, evaluateDate, nstate);
    }

    @Override
    public int update(String evaluateContent, LocalDateTime evaluateDate, Integer evaluateId) {
        return evaluateDao.update(evaluateContent, evaluateDate, evaluateId);
    }

    @Override
    public int delete(Integer evaluateId) {
        return evaluateDao.delete(evaluateId);
    }

    private void getEvaluateAdditionalData(List<EvaluateEntity> evaluateEntityList) {
        Integer[] ids = getDetailIds(evaluateEntityList);
        List<ProductEntity> attributeList = productDao.getProductAndAttributeList(ids);
        for (EvaluateEntity evaluateEntity : evaluateEntityList) {
            for (ProductEntity productEntity : attributeList) {
                if(productEntity.getProductDetailID()==evaluateEntity.getProductDetailID()) {
                    AttributeModel model = JsonUtils.readValue(productEntity.getAttributeList(),
                            AttributeModel.class);
                    evaluateEntity.setAttributeList(model.getMemory()+" "+model.getColor());
                    evaluateEntity.setProductName(productEntity.getProductName());
                    evaluateEntity.setProductPictureList(productEntity.getProductPictureList());
                }
            }
        }
    }
}
